/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display.gui;

import deatax.Improvement;
import deatax.Player;
import deatax.Resource;
import deatax.ResourceAmount;
import deatax.Settlement;
import deatax.Tile;
import deatax.unit.Unit;
import deatax.unit.UnitStack;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.util.ArrayList;
import javax.swing.JMenuItem;

/**
 *
 * @author Pieman
 */
public class ContextItem extends JMenuItem {

    private static final int PREF_WIDTH = 250;
    private static final int PREF_HEIGHT = 30;
    private boolean enabled;
    private ArrayList<Requirement> reqs;

    public ContextItem(Tile tile, Improvement imp, Player player) {
        reqs = new ArrayList<Requirement>();
        enabled = true;
        if (imp == null) {

            setText("Demolish " + tile.getImprovement().getName());
        } else {

            setText((tile.getImprovement() == null ? "Build " : "Upgrade to ") + imp.getName());
            for (ResourceAmount res : imp.getCosts()) {
                Requirement req = new Requirement(res.getResource(), res.getAmount(), player);
                reqs.add(req);
                enabled = enabled && req.met;
            }
            if (!enabled) {
                this.setEnabled(false);
            }
        }
        this.setPreferredSize(new Dimension(PREF_WIDTH, PREF_HEIGHT * (((reqs.size() - 1) / 2) + 1)));
    }

    public ContextItem(Tile tile, UnitStack units, Player player, int num) {
        reqs = new ArrayList<Requirement>();
        enabled = true;
        setText("Move " + (num > 1 ? "All " : "") + units.getType().getName() + (num > 1 ? ("s (" + num + ")") : ""));
        this.setPreferredSize(new Dimension(PREF_WIDTH, PREF_HEIGHT * (((reqs.size() - 1) / 2) + 1)));
    }

    public ContextItem(Tile tile, Improvement imp, Unit unit, Player player) {

        reqs = new ArrayList<Requirement>();
        enabled = true;
        setText("Build " + unit.getName());
        for (ResourceAmount res : unit.getCost()) {
            Requirement req = new Requirement(res.getResource(), res.getAmount(), player);
            reqs.add(req);
            enabled = enabled && req.met;
        }
        if (unit.getPopulationCost() > 0) {
            Requirement req = new Requirement(Settlement.getPopulationImage(), unit.getPopulationCost());
            reqs.add(req);
            if (tile.getPopulation() >= unit.getPopulationCost()) {
                req.setMet(true);
            } else {
                enabled = false;
            }
        }
        if (!enabled) {
            this.setEnabled(false);
        }
        this.setPreferredSize(new Dimension(PREF_WIDTH, PREF_HEIGHT * (((reqs.size() - 1) / 2) + 1)));
    }

    public ContextItem(String label, Player player, ArrayList<ResourceAmount> cost) {
        setText(label);
        enabled = true;
        reqs = new ArrayList<Requirement>();
        if (cost != null) {
            for (ResourceAmount res : cost) {
                Requirement req = new Requirement(res.getResource(), res.getAmount(), player);
                reqs.add(req);
                enabled = enabled && req.met;
            }
        }
        if (!enabled) {
            this.setEnabled(false);
        }
        this.setPreferredSize(new Dimension(PREF_WIDTH, PREF_HEIGHT));
    }

    public ContextItem(String label) {
        setText(label);
        enabled = true;
        reqs = new ArrayList<Requirement>();
        this.setPreferredSize(new Dimension(PREF_WIDTH, PREF_HEIGHT));
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        if (enabled) {
            setForeground(Color.BLACK);
        } else {
            setForeground(Color.RED);
        }
        super.paintComponent(g);

        int i = 0;
        FontMetrics fm = this.getFontMetrics(this.getFont());

        int base = ((PREF_HEIGHT - (fm.getAscent() + fm.getDescent())) / 2) + fm.getAscent() - fm.getDescent();
        //System.out.println(this.getBaseline(200, 30) + ", " + fm.getAscent() + "  " + fm.getDescent());
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
        for (Requirement req : reqs) {
            if (req.met) {
                g2d.setColor(Color.BLACK);
            } else {
                g2d.setColor(Color.RED);
            }

            int lblx = PREF_WIDTH - 30;

            int lbly = PREF_HEIGHT * (i / 2) + base;
            if (i % 2 == 1) {
                lblx -= 50;
            }

            int iconx = lblx - 20;
            int icony = (PREF_HEIGHT * (i / 2)) + ((PREF_HEIGHT - 20) / 2);

            if (req.r != null) {
                g2d.drawImage(req.r.getIcon(), iconx, icony, null);
            } else if (req.ico != null) {
                g2d.drawImage(req.ico, iconx, icony, null);
            }
            g2d.drawString(String.valueOf(req.n), lblx, lbly);
            i++;
        }

        //g2d.drawString(label, 10, 20);

    }

    private class Requirement {

        Image ico;
        Resource r;
        int n;
        boolean met;

        public Requirement(Resource r, int n, Player p) {
            this.r = r;
            this.n = n;
            if (p.getResourceNum(r) < n) {
                met = false;
            } else {
                met = true;
            }
        }

        public Requirement(Image ico, int n) {
            this.r = null;
            this.ico = ico;
            this.n = n;
            this.met = false;
        }

        public void setMet(boolean met) {
            this.met = met;
        }

        public boolean check(Player p) {
            return met;
        }
    }
}
